package com.easyedu.teacher.controller;

import com.easyedu.core.entity.po.Classroom;
import com.easyedu.core.util.R;
import com.easyedu.teacher.entity.dto.request.ReqCourseExam;
import com.easyedu.teacher.service.ExamService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lmr</ br>
 * 考试管理
 **/
@RestController
@RequestMapping("/teacher")
public class ExamController {

    private final ExamService examService;

    public ExamController(ExamService examService) {
        this.examService = examService;
    }

    /**
     * 功能描述: 获取空闲教室</br>
     * 查询的是 课程开课时间表 open_course_time 和 考试申请、审批表 exam_plan 进行UNION联合查询
     *
     * @param year     1 open_year 开放年份
     * @param semester 2 开放学期(上学期在下半年，下学期在上半年） semester
     * @param week     3 申请考试周 exam_week 开课周
     * @param day      4 申请考试天 open_day 开课星期
     * @param lesson   5 考试节次 lesson 开课节次
     * @return : com.easyedu.api.util.R
     **/
    @GetMapping("/classroom/unuse")
    public R getUnUseClassroom(@RequestParam int year, @RequestParam String semester, @RequestParam int week,
                               @RequestParam int day, @RequestParam int lesson) {
        List<Classroom> unUserClassroom = examService.getUnUserClassroom(year, semester, week, day, lesson);
        return R.ok().data(unUserClassroom);
    }

    /**
     * 功能描述: 申请考试时间
     * 查询的是  学年开课表 open_course
     *
     * @param reqCourseExam 1 学年开课插入数据
     * @return : com.easyedu.api.util.R
     **/
    @PostMapping("/exam")
    public R setCourseExamTime(@RequestBody @Valid ReqCourseExam reqCourseExam, BindingResult bindingResult) {
        //请求数据验证
        if (bindingResult.hasErrors()) {

            List<ObjectError> allErrors = bindingResult.getAllErrors();
            List<String> msgList = new ArrayList<>();
            for (ObjectError objectError : allErrors) {
                msgList.add(objectError.getDefaultMessage());
            }
            System.out.println("bindingResult.getAllErrors().get(0).getDefaultMessage() = " + allErrors);
            return R.error().message(msgList.toString());
        }

        examService.setCourseExamTime(reqCourseExam);
        return R.ok().message("申请考试时间成功");
    }
}
